package com.jeeplus.modules.guide.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.utils.PageBean;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.mapper.MobileBusinessMapper;
import com.jeeplus.modules.guide.mapper.MobileItemMapper;
import com.jeeplus.modules.guide.mapper.MobileMatterMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@RequiredArgsConstructor
@Slf4j
@Service
@Transactional(readOnly = true)
public class MobileMatterService {
    private final MobileMatterMapper mobileMatterMapper;
    private final MobileBusinessMapper mobilebusinessmapper;
    private final MobileItemMapper mobileItemmapper;
    /*
     *获取当前最新第三方事项
     */
    @Transactional(readOnly = false)
    public void insertMatterList(String version ,List<MobileMatter> list) {
        mobileMatterMapper.insertMatterList(version,list);
    }

    /*
     *获取当前最新版本号
     */
    @Transactional(readOnly = false)
    public void insertMatterAddress(List<MobileMatterAddress> list, String version) {
        mobileMatterMapper.insertMatterAddress(list,version);
    }

    public int findMatterValueCount(String version){
        return  mobileMatterMapper.findMatterValueCount(version);
    }
    public int findMatterAddressCount(String version){
        return  mobileMatterMapper.findMatterAddressCount(version);
    }


    public PageBean<MobileMatter> getMatterList(Map<String,Object> map){
        PageBean<MobileMatter> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        PageHelper.orderBy(" isTop desc, name asc");
        List<MobileMatter> list = mobileMatterMapper.getMatterList(map);
        PageInfo<MobileMatter> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    public PageBean<MobileAllMatter> getOutMatterEN(Map<String,Object> map){
        PageBean<MobileAllMatter> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        List<MobileAllMatter> list = mobileMatterMapper.getOutMatterENList(map);
        PageInfo<MobileAllMatter> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }


    public Map<String,Object> getMatterTop(String oid){
       return mobileMatterMapper.getMatterTop(oid);
    }

    @Transactional(readOnly = false)
    public void editMatterTop(String oid,int sort,String isTop,String isUpdate){
        if("true".equals(isUpdate)){
            mobileMatterMapper.editMatterTop(oid,sort,isTop);
        }else{
            mobileMatterMapper.insertMatterTop(oid,sort,isTop);
        }
    }

    @Transactional(readOnly = false)
    public void editMatterType(String typeId,String ids,String areaId,int type,String pid){
        String idArray[] =ids.split(",");
        List<MobileMatterType> list = new ArrayList<MobileMatterType>();
        for (int i=0;i<idArray.length;i++){
            MobileMatterType mt = new MobileMatterType();
            mt.setTypeId(typeId);
            mt.setOid(idArray[i]);
            mt.setAreaId(areaId);
            mt.setType(type);
            mt.setPid(pid);
            list.add(mt);
            mobileMatterMapper.deleteMatter(typeId,idArray[i],areaId,type);
        }
        mobileMatterMapper.insertMatterType(list);
    }

    @Transactional(readOnly = false)
    public void deleteMatter(String typeId,String ids,String areaId,int type){
        String idArray[] =ids.split(",");
        for (int i=0;i<idArray.length;i++){
            mobileMatterMapper.deleteMatter(typeId,idArray[i],areaId,type);
        }
    }


    public List<Map<String,Object>> getBusinessDay(String typeId,String areaId){
        return mobileMatterMapper.getBusinessDay(typeId,areaId);
    }

    public List<Map<String,Object>> getBusinessMatter(String typeId,String areaId,String version,int type){
        List<Map<String,Object>> list = null;
        if(type == 3){
            list = mobileMatterMapper.getThingMatter(typeId,areaId,type);
        }else{
            list = mobileMatterMapper.getBusinessMatter(typeId,areaId,version,type);
        }
        for (Map<String,Object> m : list){
            m.put("isBind",true);
        }
        return list;
    }

    @Transactional(readOnly = false)
    public void insertMatterLabel(String id,String oid,String matterName,String label){
        mobileItemmapper.deleteMatterLabelValue(oid,label);
        mobileMatterMapper.insertMatterLabel(id,oid,matterName,label);
    }



    public List<Map<String,Object>> getMatterTopList(String version){
        return mobileMatterMapper.getMatterTopList(version);
    }

    public Map<String,Object> findMatterList(String label,String version){
        return mobileMatterMapper.findMatterList(label,version);
    }

    public Map<String,Object> getMatterBSZN(String oid, String version){
        return mobileMatterMapper.getMatterBSZN(oid,version);
    }
    @Transactional(readOnly = false)
    public void insertBusinessDay(String isUpdate,String typeId,String areaId,String dayDate){
        JSONArray jData = JSONArray.fromObject(dayDate);
        List<MobileBusinessDay> list = JSONArray.toList(jData,new MobileBusinessDay(),new JsonConfig());
        if("false".equals(isUpdate)){
            for(MobileBusinessDay bs : list){
                bs.setId(UUID.randomUUID().toString());
            }
            mobileMatterMapper.insertBusinessDay(typeId,areaId,list);
        }else{
            for(MobileBusinessDay bs : list){
                mobileMatterMapper.updateBusinessDay(typeId,areaId,bs.getTimeData(),bs.getNumData());
            }
        }
    }

    public List<Map<String,Object>> getMatterMaterialList (String oid,String version){
        return mobileMatterMapper.getMatterMaterialList(oid,version);
    }

    /**
     * 获取事项办理状态
     * getMatterType
     * @return 受影响的行数
     */
    public List<String> getMatterType (@Param("oid")String oid,@Param("version")String version){
        return mobileMatterMapper.getMatterType(oid,version);
    }

    /**
     * findAreaType 点位类型集合
     *
     * @author azj
     * @version 2020-04-24
     */
    public List<Map<String,String>> findAreaType(){
        return mobilebusinessmapper.findAreaType();
    }

    /**
     * findAreaTypeByorgrizeStatus 查询点位类型名称
     *
     * @author azj
     * @version 2020-04-24
     */
    public String findAreaTypeByorgrizeStatus(String orgrizeStatus){
        return mobilebusinessmapper.findAreaTypeByorgrizeStatus(orgrizeStatus);
    }

    @Transactional(readOnly = false)
    public void delBussinessArea(String areaId){
        mobilebusinessmapper.delBussinessArea(areaId);
    }

    @Transactional(readOnly = false)
    public void InsertOneThing(List<MobileThing> list){
        mobileMatterMapper.delThing();
        mobileMatterMapper.InsertOneThing(list);
    }

    /**
     * 添加第三方街道网上办办件状态数据
     * intoStreetValue
     * @returnid
     */
    @Transactional(readOnly = false)
    public int intoStreetValue(String id, String areaName, String status, int code, int areaType, String title, String startTime,String wxId, String tell){
        String result = mobileMatterMapper.findStreetValue(id,areaType);
        if(StringUtils.isBlank(result)){
            return mobileMatterMapper.intoStreetValue(id,areaName,status,code,areaType,title,startTime,wxId,tell);
        }else{
            return -1;
        }

    }

    /**
     * 更新第三方街道网上办办件状态数据
     * updateStreetValue
     * @returnid
     */
    @Transactional(readOnly = false)
    public int updateStreetValue(String status, int code, String updateTime, String id, int areaType){
        return mobileMatterMapper.updateStreetValue(status,code,updateTime,id,areaType);
    }

    public Integer isDoTime(){
        return mobileMatterMapper.isDoTime();
    }

    @Transactional(readOnly = false)
    public void intoStreetValueOther(MobileStreet ms){
        mobileMatterMapper.deleteStreetValueOther(ms.getSnid());
        mobileMatterMapper.intoStreetValueOther(ms);
    }
    public List<MobileStreet> findStreetValueStart(String userid){
        return mobileMatterMapper.findStreetValueStart(userid);
    }
    public List<MobileStreet> findStreetValueEnd(String userid){
        return mobileMatterMapper.findStreetValueEnd(userid);
    }

    public String getWSAreaIdAndTypeId(String oid){
        return mobileMatterMapper.getWSAreaIdAndTypeId(oid);
    }

    public List<String> findAreaIdByOid(String oid,List<String> list){
        return mobileMatterMapper.findAreaIdByOid(oid,list);
    }
    public String findWXIdByIdcard(String idcard) {
        return mobileMatterMapper.findWXIdByIdcard(idcard);
    }

  }
